{include file="public/header" /}
<div class="header">
	<span>演示列表-方法渲染</span>
	<div></div>
</div>
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist" style="height:100%;">
	<div class="layui-form-item" id="search"></div>
</div>
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<div class="layui-btn-group">
			<button type="button" class="layui-btn layui-btn-primary" lay-event="add">
				<i class="layui-icon layui-icon-add-1"></i>添加
			</button>
			<button type="button" class="layui-btn layui-btn-primary" lay-event="edit">
				<i class="layui-icon layui-icon-edit"></i>修改
			</button>
			<button type="button" class="layui-btn layui-btn-primary" lay-event="del">
				<i class="layui-icon layui-icon-delete"></i>删除
			</button>
		</div>
	</div>
</script>
<table class="layui-hide" id="datalist" lay-filter="datalist"></table>
<script type="text/javascript">
	layui.use(['layer','table'],function(){
		table = layui.table;
		layer = layui.layer;
		$ = layui.jquery;
		get_data();

		table.on('row(datalist)',function(obj){
			obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
			obj.tr.find("div.layui-unselect.layui-form-radio")[0].click();
		});
		table.on('toolbar(datalist)',function(obj){
			var checkStatus = table.checkStatus(obj.config.id);
			var data = checkStatus.data;
			var datas = obj.data;
			var layEvent = obj.event;
			var tr = obj.tr;

			switch(obj.event){
				case 'add':
					layer.full(layer.open({
						title: '添加',
						type: 2,
						content: "{$config['admin_route']}bewtest/test_add",
						maxmin: true,
						area: ['70%','70%'],
						btn: ['确定','关闭'],
						yes: function(index,layero){
							var form = layero.find('iframe')[0].contentWindow;
							var body = layer.getChildFrame('body', index);
							var test_img =[];
							body.find("input[name='test_img']").each(function(){
								test_img.push($(this).val());
							})
							var dataj = {
								test_input : form.test_input.value,
								test_reference : form.test_reference.value,
								test_time : form.test_time.value,
								test_data : form.test_data.value,
								test_datatime : form.test_datatime.value,
								test_img : test_img,
								test_rich : form.test_rich.value,
								test_rich_baidu : form.test_rich_baidu.value,
								test_url : form.test_url.value
							};
							$.post('{$config["admin_route"]}bewtest/test_add',dataj,function(res){
								if(res.code>0){
									layer.msg(res.msg,{'icon':2});
								}else{
									layer.msg(res.msg,{'icon':1});
									get_data();
									layer.close(index);
								}
							},'json');
						}
					}))
					break;
				case 'edit':
					if(!data[0]){
						layer.msg('请选择一条数据',{'icon':2});
						return false;
					}
					layer.full(layer.open({
						title: '修改',
						type: 2,
						content: "{$config['admin_route']}bewtest/test_edit?test_id="+data[0].test_id,
						maxmin: true,
						area: ['70%','70%'],
						btn: ['确定','关闭'],
						yes: function(index,layero){
							var form = layero.find('iframe')[0].contentWindow;
							var body = layer.getChildFrame('body', index);

							var test_img =[];
							body.find("input[name='test_img']").each(function(){
								test_img.push($(this).val());
							})
							var dataj = {
								test_id : data[0].test_id,
								test_input : form.test_input.value,
								test_reference : form.test_reference.value,
								test_time : form.test_time.value,
								test_data : form.test_data.value,
								test_datatime : form.test_datatime.value,
								test_img : test_img,
								test_rich : form.test_rich.value,
								test_rich_baidu : form.test_rich_baidu.value,
								test_url : form.test_url.value
							};
							$.post('{$config["admin_route"]}bewtest/test_edit',dataj,function(res){
								if(res.code>0){
									layer.msg(res.msg,{'icon':2});
								}else{
									layer.msg(res.msg,{'icon':1});
									get_data();
									layer.close(index);
								}
							},'json');
						}
					}))
					break;
				case 'del':
					if(!data[0]){
						layer.msg('请选择一条数据',{'icon':2});
						return false;
					}
					layer.confirm('确定要删除吗？',{
						icon:3,
						btn: ['确定','取消']
					},function(){
						$.post('{$config["admin_route"]}bewtest/test_del',{'test_id':data[0].test_id},function(res){
							if(res.code>0){
								layer.alert(res.msg,{icon:2});
							}else{
								layer.msg(res.msg);
								setTimeout(function(){window.location.reload();},1000);
							}
						},'json');
					});
				break;
			};
		});

		function get_data(){
			table.render({
				elem: '#datalist',
				method : 'post'
				,url:"{$config['admin_route']}bewtest/test_list"
				,toolbar: '#toolbar'
				,defaultToolbar: ['filter', 'print', 'exports', {
					title: '提示'
					,layEvent: 'LAYTABLE_TIPS'
					,icon: 'layui-icon-tips'
				}]
				,title: '演示列表'
				,page: true
				,cols: [[
					{type:'checkbox'},
					{type:'radio'}
					,{field:'test_id', width:50, title:'ID'}
					,{field:'test_input', width:100, title: '文本'}
					,{field:'test_rich', width:100, title: '富文本'}
					,{field:'test_rich_baidu', width:120, title: '百度文本'}
					,{field:'test_img', width:100, title: '图片',templet:function(res){
						return '<img style="width:30px;height:30px;" onmouseover="show_img(this)" onmouseleave="hide_img()" src="'+res.test_img+'"';
					}}
					,{field:'test_reference', width:100, title: '参照',templet:function(res){
						if(res.test_reference == 1){
							return '<span style="color:green;">开启</span>';
						}else{
							return '<span style="color:grey;">关闭</span>';
						}
					}}
					,{field:'test_time', width:100, title: '时间戳'}
					,{field:'test_data', width:100, title: '日期'}
					,{field:'test_datatime', width:150, title: '日期时间'}
					,{field:'test_url', width:150, title: '网址链接',templet:function(res){
						return '<a href="'+res.test_url+'" target="_blank">'+res.test_url+'</a>';
					}}
				]]
				,data:[]
				,limit :10
				,limits:[10,20,30]
				,height: 'full-100'
			});
		}
	});
</script>
{include file="public/tail" /}